using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using SuperMarketManagement.DAO.NHibernate;
using SuperMarketManagement.Entity;

namespace SuperMarketManagement.Web.Controllers
{
    [Authorize(Roles = "admin,customer-manager,sale,sale-manager")]
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult List()
        {
            DAOFactory factory = new DAOFactory();
            IList<Customer> customers = factory.CustomerDAO.GetAll();
            return View(customers);
        }

        [Authorize(Roles = "admin,customer-manager")]
        public ActionResult Edit()
        {
            DAOFactory factory = new DAOFactory();
            long customerId = Convert.ToInt64(Request["id"]);
            Customer customer = customerId != 0 ? factory.CustomerDAO.GetById(customerId) : new Customer();

            if (Request.RequestType.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {//an update command was initiated
                customer.Name = Convert.ToString(Request["name"]);
                customer.PersonalId = Convert.ToString(Request["personalId"]);
                customer.Cellphone = Convert.ToString(Request["cellphone"]);
                customer.CreatedDate = DateTime.Now;
                factory.CustomerDAO.Save(customer);
                return RedirectToAction("List");
            }
            return View(customer);
        }

        public ActionResult Info()
        {

            DAOFactory factory = new DAOFactory();
            long customerId = Convert.ToInt64(Request["id"]);
            Customer customer = factory.CustomerDAO.GetById(customerId);
            ViewData["invoices"] = factory.InvoiceDAO.GetByCustomer(customer);
            return View(customer);
        }

    }
}
